package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.ReturnStatus;
import com.ygqh.baby.ao.ReturnType;
import com.ygqh.baby.mapper.YgReturnOrderLogMapper;
import com.ygqh.baby.po.YgReturnOrderExample;
import com.ygqh.baby.po.YgReturnOrderLog;
import com.ygqh.baby.po.YgReturnOrderLogExample;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgReturnOrderLogService;
import com.ygqh.baby.service.YgUserService;

@Service
public class YgReturnOrderLogServiceImpl implements YgReturnOrderLogService {

	@Autowired
	private YgReturnOrderLogMapper ygReturnOrderLogMapper;
	@Autowired
	private YgUserService ygUserService;

	@Override
	public List<YgReturnOrderLog> find() {
		YgReturnOrderLogExample example = new YgReturnOrderLogExample();
		example.createCriteria();
		return ygReturnOrderLogMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgReturnOrderLogExample example = new YgReturnOrderLogExample();
		example.createCriteria();
		return ygReturnOrderLogMapper.countByExample(example);
	}

	@Override
	public YgReturnOrderLog findById(Long id) {
		return ygReturnOrderLogMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgReturnOrderLog ygReturnOrderLog) {
		return ygReturnOrderLogMapper.insertSelective(ygReturnOrderLog);
	}

	@Override
	public int update(YgReturnOrderLog ygReturnOrderLog) {
		return ygReturnOrderLogMapper.updateByPrimaryKeySelective(ygReturnOrderLog);
	}

	@Override
	public ResultSet<YgReturnOrderLog> search(QueryInfo queryInfo, String q) {
		List<YgReturnOrderLog> list = ygReturnOrderLogMapper.selectSuper(queryInfo, q);
		int count = ygReturnOrderLogMapper.countSuper(q);
		ResultSet<YgReturnOrderLog> resultSet = new ResultSet<YgReturnOrderLog>(count, list);
		return resultSet;
	}

	@Override
	public int saveOrUpdate(YgReturnOrderLog ygReturnOrderLog) {
		int r = 0;
		if (ygReturnOrderLog.getId() != null) {
			r = this.update(ygReturnOrderLog);
		} else {
			r = this.save(ygReturnOrderLog);
		}
		return r;
	}

	@Override
	public List<YgReturnOrderLog> getReturnScheduleList(String returnCode) {
		YgReturnOrderLogExample example = new YgReturnOrderLogExample();
		example.createCriteria().andReturnCodeEqualTo(returnCode);
		example.setOrderByClause("create_time desc");
		return ygReturnOrderLogMapper.selectByExample(example);
	}

	@Override
	public int addReturnOrderLog(String returnCode, String operation, ReturnStatus returnStatus, Long userId, String remark) {
		YgUser user = ygUserService.findById(userId);
		return setLog(returnCode, operation, returnStatus, user.getUserName(), null);
	}

	@Override
	public int addReturnOrderLog(String returnCode, String operation, ReturnStatus returnStatus, String createBy, String remark) {
		return setLog(returnCode, operation, returnStatus, createBy, remark);
	}

	@Override
	public YgReturnOrderLog findByReturnCode(String returnCode) {
		YgReturnOrderLogExample example = new YgReturnOrderLogExample();
		example.createCriteria().andReturnCodeEqualTo(returnCode);
		List<YgReturnOrderLog> list = ygReturnOrderLogMapper.selectByExample(example);
		if (list.isEmpty())
			return null;
		return list.get(0);
	}

	@Override
	public YgReturnOrderLog findByReturnCode(String returnCode, ReturnStatus returnStatus) {
		YgReturnOrderLogExample example = new YgReturnOrderLogExample();
		example.createCriteria().andReturnCodeEqualTo(returnCode).andReturnStatusEqualTo(returnStatus.name());
		List<YgReturnOrderLog> list = ygReturnOrderLogMapper.selectByExample(example);
		if (list.isEmpty())
			return null;
		return list.get(0);
	}

	private int setLog(String returnCode, String operation, ReturnStatus returnStatus, String createBy, String remark) {
		YgReturnOrderLog log = new YgReturnOrderLog();
		log.setReturnCode(returnCode);
		log.setOperation(operation);
		log.setReturnStatus(returnStatus);
		log.setCreateTime(new Date());
		log.setCreateBy(createBy);
		log.setRemark(remark);
		return this.save(log);
	}

	@Override
	public int addReturnOrderLogBatch(List<String> returnCodes, String operation, ReturnStatus returnStatus, String createBy) {
		return ygReturnOrderLogMapper.addReturnOrderLogBatch(returnCodes, operation, returnStatus, createBy);
	}

	@Override
	public List<YgReturnOrderLog> findReturnLogByOrderId(Long orderId) {
		return ygReturnOrderLogMapper.selectReturnLogByOrderId(orderId);
	}

	@Override
	public int addBatch(List<YgReturnOrderLog> returnLogs) {
		if(CollectionUtils.isEmpty(returnLogs)){
			return 0 ;
		}
		return ygReturnOrderLogMapper.addBatch(returnLogs);
	}


}
